﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "教学运行管理 - 考试安排 - 期末课程指定时间");
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		
		termId = event.getArg("TermID");
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");

		examId = event.getArg("EXID"); /* 考试任务号 */

		buildingId = event.getArg("BID"); /* 教学楼号 */
		
		currentTab = event.getArg("TabID", "labArrange");
		
		passArgs = structNew();
		
		/* 加载考试任务 */
		sql = "SELECT 
					t_examtask.task_prog,  
					t_examtask.wid, 
					t_examtask.wdidx,
					t_examtask.cellidx, 
					t_examtask.campus_id, 
					t_course.course_name,  
					t_examtask.examer_sum 
				FROM 
					t_examtask 
					INNER JOIN t_course ON t_course.cid = t_examtask.cid 
				WHERE 
					t_examtask.task_exid = :taskExid ";
					
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="taskExid", value=examId, cfsqltype="cf_sql_char" );
		
		rs_examTask = queryObj.execute( sql=sql ).getResult();
		
		rs_clsroom = queryNew("cr_id, b_name, cr_floor, classroom, sits_exam, cr_type_name");
		rs_building = queryNew("b_id, b_name, b_floors");
		
		
		if ( rs_examTask.task_prog eq "1" ) {
			
			/* 读取可用教室列表 */
			examWeek = int(rs_examTask.wid);
			examWeekDay = rs_examTask.wdidx;
			examCell = rs_examTask.cellidx;
			campusId = rs_examTask.campus_id;	


			/* 加载考试安排校区的教学楼列表 */
			sql = "SELECT b_id, b_name, b_floors FROM t_building WHERE campus_id = :campusId ";
		
			queryObj = new query( datasource=application.dnsSlave );
			queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
		
			rs_building = queryObj.execute( sql=sql ).getResult();
		
					
			examWeekIdx = mathAdvice.convertExamWeekIdx(examWeek);
			
			cells = 2;
			/* 如果为 第二讲 或 第五讲 则使用最长学时数 */
			if (examCell eq 2 or examCell eq 5) { cells = 3; }
					
			cellStart = mathAdvice.convertLectureIdxToCellStart(examCell);
			cellEnd = cellStart + cells -1;
			
			if ( len(buildingId) ) {
				
				sql = "SELECT 
							a.cr_id, a.cr_floor, a.classroom, a.sits_exam, 
							c.b_name, 
							d.cr_type_name 
						FROM 
							t_classroom a 
							INNER JOIN t_building c ON c.b_id = a.b_id 
							INNER JOIN t_classroom_type d ON d.cr_type = a.cr_type 
						WHERE 
							a.campus_id = :campusId 
							AND 
							c.b_id = :buildingId 
							AND 
							NOT EXISTS(
								SELECT 1 FROM t_clsroom_timetable b 
								WHERE 
									b.cr_id = a.cr_id 
									AND 
									b.term_id = :termId 
									AND 
									b.wdidx = :weekDay 
									AND 
									NOT (b.cellidx + b.cellrelay - 1 < :cellStart OR b.cellidx > :cellEnd) 
									AND 
									SUBSTR(b.widx, " & examWeek & ", 1) = '1' ) 
						ORDER BY 
							 a.cr_floor, a.classroom ";
				
				queryObj = new query( datasource=application.dnsSlave ); 
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="weekDay", value=examWeekDay, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="cellEnd", value=cellEnd, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="buildingId", value=buildingId, cfsqltype="cf_sql_char" );
			
				rs_clsroom = queryObj.execute( sql=sql ).getResult();
				
			}
			else {
			
				sql = "SELECT 
							a.cr_id, a.cr_floor, a.classroom, a.sits_exam, 
							c.b_name, 
							d.cr_type_name 
						FROM 
							t_classroom a 
							INNER JOIN t_building c ON c.b_id = a.b_id 
							INNER JOIN t_classroom_type d ON d.cr_type = a.cr_type 
						WHERE 
							SUBSTR(a.cr_usage, 3, 1) = '1' 
							AND 
							a.campus_id = :campusId 
							AND 
							NOT EXISTS( 
								SELECT 1 FROM t_clsroom_timetable b 
								WHERE 
									b.cr_id = a.cr_id 
									AND 
									b.term_id = :termId 
									AND 
									b.wdidx = :weekDay 
									AND 
									NOT (b.cellidx + b.cellrelay - 1 < :cellStart OR b.cellidx > :cellEnd) 
									AND 
									SUBSTR(b.widx, " & examWeek & ", 1) = '1' ) 
						ORDER BY 
							 a.sits_exam DESC, c.b_id, a.cr_floor, a.classroom ";
			
				queryObj = new query( datasource=application.dnsSlave, maxRows=15 ); 
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="weekDay", value=examWeekDay, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="cellEnd", value=cellEnd, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
			
				rs_clsroom = queryObj.execute( sql=sql ).getResult();
			
			}
			
		}
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('examMidArrange')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">期中考试安排</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 building"></i>《<cfoutput>#rs_examTask.course_name#</cfoutput>》 - 指定考试场地
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix">
				
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labArrange">class="active"</cfif> id="labArrange" tabTarget="Arrange">考试场地</span>
						<span <cfif currentTab eq "labSpecial">class="active"</cfif> id="labSpecial" tabTarget="Special">指定考试区域</span>
					</div>
				</div>
				
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					// -->
					//]]>
				</script>
				
				<div id="Arrange" class="tabContent">
					
					<div class="noticeBlock">
						
						<h3>《<cfoutput>#rs_examTask.course_name#</cfoutput>》</h3>
						<p>
							<span class="img icon16x16 message"></span><em><cfoutput>#academicYear#</cfoutput>-<cfoutput>#academicTerm#</cfoutput></em>学期期中考试安排.
							该门课程考试计划安排于 <em><cfoutput>#rs_examTask.wid#</cfoutput></em> 周,
							考试时间: <cfoutput>#mathAdvice.convertWeekDayToChinese( rs_examTask.wdidx )#</cfoutput><cfoutput>#mathAdvice.convertExamIdxToChinese( rs_examTask.cellidx )#</cfoutput>, 
							计划有<em><cfoutput>#rs_examTask.examer_sum#</cfoutput></em>人需要安排考试.
						</p>
					</div>

					<div id="arrangeAutoBox" <cfif len(buildingId)>class="hidden_elem"</cfif> >
						
						<form id="formAuto" class="formWrapper" method="post" action="<cfoutput>#buildURL('examMidTaskSetClassroom')#</cfoutput>">
							
							<div class="label">
								<span class="req">*</span><b>教室</b>
								<div class="secondRow">自动选择</div>
							</div>
							
							<hr/>
							
							<cfif rs_clsroom.recordCount>
									<div class="lable">
										<input id="triggerAuto" class="button" type="button" value="自动安排考试教室" />
										<input id="formAutoReset" class="button2" type="button" value="指定教室" />
									</div>
								<cfelse>
									<div class="systemNotice">
										<h3><span class="img icon16x16 info"></span>没有可用的考场</h3>
										<p>在您当前设置的考试时间, 已经没有考场可以进行考试安排, <a href="<cfoutput>#buildURL('examMidArrange')#</cfoutput>">点这里返回考试安排</a></p>
									</div>
							</cfif>
							
						</form>
						
						<div class="noticeBlock" class="hidden_elem">
							<div id="logger"></div>
							<hr/>
						</div>
						<div id="outer"></div>
						
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							
							$(document).ready(function() {
								
								$("#formAutoReset").click(function(){
								
									$("#roomBox").removeClass("hidden_elem");
									$("#arrangeAutoBox").addClass("hidden_elem");
									
								});
								
								$("#triggerAuto").click(function(){
									
									/* 自动安排场地阻挡 */
									$.blockUI({ 
											message: '正在自动安排场地...',
											css: { 
												'width': '22%',
												'left': '42%',
												'border': 'none', 
												'padding': '15px 5px', 
												'backgroundColor': '#333', 
												'border-radius': '4px', 
												'opacity': .8, 
												'color': '#fff',
												'cursor': null
												},
										overlayCSS: {
												'backgroundColor': '#000', 
												'opacity': .1 ,
												'cursor': null
												}
										});
									
									$.post(
											"<cfoutput>#buildURL('apiExamMidTaskSetPlace')#</cfoutput>",
											{
												'EXID': '<cfoutput>#examId#</cfoutput>',
												'seed': (new Date()).getTime()	
											},
											function(data) {
												
												//$("#out").html(data);
												
												result = $.parseJSON(data);
												
												if (result.success) {
													
													/* 刷新排课数据 */
													//window.location.reload();
													$("#formAuto").addClass("hidden_elem");

													var logger = $("<ul></ul>");
													
													for ( var e=0; e < result.Progress.length; e++ ) {
														$("<li></li>").html(result.Progress[e]).appendTo(logger);
													}
																										
													$("#logger").html(logger);
													
													var tabler = $("<table></table>").addClass("UIEditable");
													var thead = $("<thead><tr><td width='20'></td><td>考场</td><td>容量</td><td>考试人数</td><td>座次编号</td></tr></thead>")
													var tbody = $("<tbody></tbody>");
													
													var i = 0;
													for ( var e=0; e < result.ClassRoom.length; e++ ) {
														i++;
														var tr = $("<tr></tr>").addClass("editRows");
														
														$("<td></td>").addClass("index").html(i).appendTo(tr);
														$("<td></td>").html(result.ClassRoom[e][0]).appendTo(tr);
														$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][1]).appendTo(tr);
														$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][2]).appendTo(tr);
														$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][3] + " - " + result.ClassRoom[e][4]).appendTo(tr);
														
														tr.appendTo(tbody);	
													}
													
													thead.appendTo(tabler);
													tbody.appendTo(tabler);
													
													$("#outer").html(tabler);
													
													/* 取消阻挡 */
													$.unblockUI();
													
													/* 延迟5秒跳转 */
													$('body').oneTime('5s',function(){
														window.location = '<cfoutput>#buildURL("examMidArrange")#</cfoutput>&TabID=labDoneCourse';
													});
													
												}
												else {
													alert(result.reason);
													
													/* 取消阻挡 */
													$.unblockUI();
												}
												
											});
									
								});
																
							});
							
							// -->
							//]]>
						</script>
						
					</div>

					<div id="roomBox" <cfif not len(buildingId)>class="hidden_elem"</cfif> >
					
						<cfif rs_clsroom.recordCount>
							
							<form id="formManual" class="formWrapper" method="post" action="<cfoutput>#buildURL('examTaskSetClassroom')#</cfoutput>">
								<input type="hidden" id="ClassroomIDs" name="ClassroomIDs" value="" />
							
								<div class="label">
									<span class="req">*</span><b>已选教室</b>
									<div id="ClassroomNames" class="secondRow"></div>
								</div>
							
								<hr/>

								<div class="lable">
									<input id="opManual" class="button" type="button" value="安排考试到选定教室" />
									<input id="formReset" class="button2" type="button" value="自动安排" />
								</div>							
							</form>
							
							<div id="selectorBox">
								<table id="roomtList" class="UIEditable">
									<thead>
										<tr>
											<td width="18"></td>
											<td>教室</td>
											<td width="100">教室类别</td>
											<td width="150">教学楼</td>
											<td width="80">楼层</td>
											<td width="50">考场座位</td>
										</tr>
									</thead>
									<tbody>
                                        <cfloop query="rs_clsroom"  >
											<tr class="editRows" rowid="<cfoutput>#rs_clsroom.cr_id#</cfoutput>">
												<td><a rowid="<cfoutput>#rs_clsroom.cr_id#</cfoutput>" usage="ForTask" class="stat no"></a></td>
												<td><span usage="ForRoomNames"><cfoutput>#rs_clsroom.classroom#</cfoutput></span></td>
												<td><cfoutput>#rs_clsroom.cr_type_name#</cfoutput></td>
												<td><cfoutput>#rs_clsroom.b_name#</cfoutput></td>
												<td><cfoutput>#dictAdvice.getFloorString(rs_clsroom.cr_floor)#</cfoutput></td>
												<td><span usage="ForSits" class="numeric"><cfoutput>#rs_clsroom.sits_exam#</cfoutput></span></td>
											</tr>
										</cfloop>
									</tbody>
								</table>
							</div>
							
							<div class="noticeBlock" class="hidden_elem">
								<div id="loggerManual"></div>
								<hr/>
							</div>
							<div id="outerManual"></div>

							<script language="javascript" type="text/javascript">
								//<![CDATA[
								<!--
								$(document).ready(function() {
									
									$("#formReset").click(function(){
										
										$("#roomBox").addClass("hidden_elem");
										$("#arrangeAutoBox").removeClass("hidden_elem");
									
									});

									$("#opManual").click(function(){
										
										var $roomSelected = $("a.yes[usage='ForTask']");
										var roomIds = [];
										var roomNames = [];
										var examSits = 0;
										
										$roomSelected.each(function(){
											
											roomIds.push( $(this).attr("rowid") );
												
											var $tr = $("tr[rowid='" + $(this).attr("rowid") + "']");
											
											roomNames.push( $("span[usage='ForRoomNames']", $tr).text() );
										
											examSits = examSits + parseInt($("span[usage='ForSits']", $tr).text());

										});

										if ( roomIds.length && examSits >= <cfoutput>#rs_examTask.examer_sum#</cfoutput> ) {

											$("#ClassroomIDs").val( roomIds.join("|") );
											$("#ClassroomNames").text( roomNames.join(", ") );

											$("#selectorBox").addClass("hidden_elem");
											
											$.post(
													"<cfoutput>#buildURL('apiExamMidTaskSetPlaceManual')#</cfoutput>",
													{
														'EXID': '<cfoutput>#examId#</cfoutput>', 
														'RoomIDs': roomIds.join("|"), 
														'seed': (new Date()).getTime()	
													},
													function(data) {
														
														//$("#outerManual").html(data);
														
														result = $.parseJSON(data);
														
														if (result.success) {
													
															/* 刷新数据 */
															//window.location.reload();
															$("#formManual").addClass("hidden_elem");

															var logger = $("<ul></ul>");
													
															for ( var e=0; e < result.Progress.length; e++ ) {
																$("<li></li>").html(result.Progress[e]).appendTo(logger);
															}
																										
															$("#loggerManual").html(logger);
													
															var tabler = $("<table></table>").addClass("UIEditable");
															var thead = $("<thead><tr><td width='20'></td><td>考场</td><td>容量</td><td>考试人数</td><td>座次编号</td></tr></thead>")
															var tbody = $("<tbody></tbody>");
													
															var i = 0;
															for ( var e=0; e < result.ClassRoom.length; e++ ) {
																i++;
																var tr = $("<tr></tr>").addClass("editRows");
														
																$("<td></td>").addClass("index").html(i).appendTo(tr);
																$("<td></td>").html(result.ClassRoom[e][0]).appendTo(tr);
																$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][1]).appendTo(tr);
																$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][2]).appendTo(tr);
																$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][3] + " - " + result.ClassRoom[e][4]).appendTo(tr);
														
																tr.appendTo(tbody);	
															}
													
															thead.appendTo(tabler);
															tbody.appendTo(tabler);
													
															$("#outerManual").html(tabler);
													
															/* 延迟5秒跳转 */
															$('body').oneTime('5s',function(){
																window.location = '<cfoutput>#buildURL('examMidArrange')#</cfoutput>&TabID=labDoneCourse';
															});
													
														}
														else {
															alert(result.reason);
														}
														
													});
											
											

										}
										else {
											alert("请选择适合的教室");
										}
										
										
									});
									
									
									$("a[usage='ForTask']", $("#roomtList")).click(function(){
										
										var rowid = $(this).attr("rowid");
										var status = $(this).hasClass("yes");
										
										var $tr = $("tr[rowid='" + $(this).attr("rowid") + "']");

										/* 切换状态数据 */
										if (status) {
											$(this).removeClass("yes").addClass("no");
										}
										else {
											$(this).removeClass("no").addClass("yes");
										}

									});
									
								});
								// -->
								//]]>				
							</script>

							<cfelse>
							
							<div class="systemNotice">
								<h3><span class="img icon16x16 info"></span>没有可用的考场</h3>
								<p>在您当前设置的考试时间, 已经没有考场可以进行考试安排, <a href="<cfoutput>#buildURL('examArrange')#</cfoutput>">点这里返回考试安排</a></p>
							</div>
							
						</cfif>
					
					</div>

				</div>
				
				<div id="Special" class="tabContent">
					
					<div class="noticeBlock">
						<h3>指定考试安排的楼栋</h3>
						<p><span class="img icon16x16 message"></span>如果需要安排考试到特殊场地, 则可以在这里选定考场所在楼栋</p>
					</div>
					
					<cfif rs_building.recordCount>
						
						<cfset structInsert( passArgs, "EXID", examId ) />
						
						<div class="UICardTable">
							<cfloop query="rs_building">
								<cfset structInsert(passArgs, "BID", rs_building.b_id, true) />
								<dl>
									<dt>
										<a href="<cfoutput>#buildURL('examMidTaskSetClassroom', passArgs)#</cfoutput>">
											<em class="building"><!--imgholder--></em>
											<dd>
												<h3><cfoutput>#rs_building.b_name#</cfoutput></h3>
												<div><cfoutput>#dictAdvice.getBuildingFloorSummary(rs_building.b_floors)#</cfoutput></div>
											</dd>
										</a>
									</dt>
								</dl>
							</cfloop>
						</div>
					</cfif>
					
				</div>
				
			</div>
		</li>
	</ul>
</div>

